#include<stdio.h>
#include<string.h>
int main()
{
    int x[3000];
    int c[3000];
    //freopen("in.txt","r",stdin);
    int t;
    scanf("%d",&t);
    for(int i = 0; i < t; i++)
    {
        int flag = 0;
        memset(x,0,sizeof x);
        memset(c,0,sizeof x);
        int a,b;
        scanf("%d %d",&a,&b);
        x[0] = 1;
        for(int j = 1; j <=a;j++)
        {
            memset(c,0,sizeof x);
            for(int m = 0; m < 3000;m++)
            {
                c[m] += x[m]*j;
                if(c[m] >= 10)
                {
                    c[m+1] += c[m]/10;
                    c[m] = c[m]%10;
                }
            }
            for(int m = 0; m < 3000;m++)
            {
                x[m] = c[m];
            }
        }
        int k = 2999;
        while (x[k] == 0 )
        {
            k--;
        }
        while(k >= 0)
        {
            //printf("%d",x[k]);
            if(x[k] == b) flag++;
            k--;
        }
        printf("%d\n",flag);
    }
}